<template>
	<view>
		<form>
			<view class="cu-form-group margin-top">
				<view class="title">手机号</view>
				<input placeholder="手机号" v-model="phone"></input>
			</view>
			<view class="cu-form-group solid-bottom">
				<view class="title">验证码</view>
				<input placeholder="输入获取到的验证码" name="input" v-model='code'></input>
				<button class='cu-btn bg-green shadow' :disabled="disab" @tap="getYzm">{{yzmStatus}}</button>
			</view>
			<view class="padding">
				<button class="cu-btn bg-blue margin-tb-sm lg" style="width: 100%;" @tap="geteditpass">登录</button>
			</view>
		</form>
	</view>
</template>

<script>
	var yzmTime=59,yzmEnable=true;
	export default {
		data() {
			return {
				yzmStatus:"获取验证码",
				disab:false,
				code:null,
				phone:null
			}
		},
		methods: {
			downTimer:function(){
				var that=this;
				var it=setInterval(function(){
					yzmEnable=false;
					that.yzmStatus="倒计时"+yzmTime+"秒";
					yzmTime--;
					if(yzmTime==0){
						yzmTime=59;
						yzmEnable=true;
						that.disab=false;
						that.yzmStatus="获取验证码";
						clearInterval(it);
					}
				},1000);
			},
			geteditpass(){
				var that=this;
				if(this.phone==null){
					that.app.showMsg('请输入手机号!');
					
					return;
				}
				if(this.code==null){
					that.app.showMsg('请输入验证码!');
					
					return;
				}
				uni.showLoading({
					mask:true
				})
				uni.request({
					url:that.app.apiHost+"/bindphone",
					method:"POST",
					header:{'Authorization':'bearer '+that.app.appToken},
					data:{code:that.code,mobile:that.phone,type:'newphone'},
					success:function(res){
            console.log(res);
						if(res.data.code == -1){
							that.app.showMsg('请登陆后再操作!','../login/login');
						}
						if(res.data.code==0){
							that.app.showMsg('登录成功!','../index/index');
              that.app.setAuthCode(that.app.appToken); //登录成功把token存本地
              console.log(221,that.app.appToken);
							setTimeout(() => {
								uni.switchTab({
									url: '/pages/index/index'
								})
							}, 1000)
						}else{
							that.app.showMsg(res.data.msg);
						}
					}
				})
			},
			getYzm:function(){
					if(!yzmEnable) return false;
					var that=this;
					uni.request({
						url:that.app.apiHost+"/sendMsg",
						method:"POST",
						header:{'Authorization':'bearer '+that.app.appToken},
						data:{phone:that.phone,type:'newphone'},
						success:function(res){
              console.log(res);
							// if(res.data.code==-1){
							// 	that.app.showMsg('请登陆后再操作!','../login/login');
							// }
							that.app.showMsg(res.data.msg);
							if(res.data.code==0){
								that.disab=true;
								that.downTimer();
							}
						}
					})
			},
		}
	}
</script>

<style>

</style>
